<?php 
namespace Weixin\Controller;
use Common\Controller\BaseController;
class MenuController extends BaseController{
	
	public function admin(){
		$m = D('WxMenu');
		$menu = $m->where('pid = 0')->order('listorder asc')->select();
		foreach($menu as $key=>$vo){
			$menu[$key]['sub_button'] = $m->where('pid = '.$vo['id'])->order('listorder asc')->select();
		}
		$this->menu = $menu;
		$this->display();
	}
	
	public function tijiao(){
		import('@.Lib.MiniWx.wechat');
		$weObj = new \Wechat(C('WEIXIN_CONFIG')); //创建实例对象
		
		$m = D('WxMenu');
		$menu1 = $m->where('pid = 0')->order('listorder asc')->select();
		$menu = array();
		foreach($menu1 as $vo){

			$tmps = $m->where('pid = '.$vo['id'])->order('listorder asc')->select();
			if($tmps){
				$dataa['name'] = $vo['name'];
				$dataa['sub_button'] = array();
				foreach($tmps as $tmp){
					$dataa['sub_button'][] = $this->getdata($tmp);
				}
				$menu[] = $dataa;
			}else{
				$menu[] = $this->getdata($vo);
			}
		}
		
		
		dump($menu);
		$result = $weObj->createMenu(array('button'=>$menu));
		dump($result);
	}
	
	public function getdata($vo){
		$data['name'] = $vo['name'];
		$data['type'] = $vo['type'];
		if($data['type'] == 'view'){
			$data['url'] = $vo['url'];
		}else{
			$data['key'] = $vo['key'];
		}
		return $data;
	}
	
	public function edit(){
		$this->types = array(
				'click' => '点击推事件',
				'view' => '跳转URL',
				'scancode_push' => '扫码推事件',
				'scancode_waitmsg' => '扫码推事件且弹出“消息接收中”提示框',
				'pic_sysphoto' => '弹出系统拍照发图',
				'pic_photo_or_album' => '弹出拍照或者相册发图',
				'pic_weixin' => '弹出微信相册发图器',
				'location_select' => '弹出地理位置选择器',
				'media_id' => '下发消息（除文本消息）',
				'view_limited' => '跳转图文消息URL',
		);
		if($_GET['id']){
			$this->info = M('wx_menu')->find($_GET['id']);
		}
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$m = M('wx_menu');
			$_POST['update_time'] = Date('Y-m-d H:i:s');
			$data = $m->create($_POST);
			if($_POST['id']){
				if($m->save($data)){
					$this->success('更新成功');
				}else{
					$this->error('更新失败');
				}
			}else{
				if($m->add($data)){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	
	public function del(){
		$this->_del(M('wx_menu'), I('ids'));
	}
}
?>